

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.logging.prefix &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../index.html" />
    <link rel="up" title="pyamp.logging" href="../logging.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../logging.html" accesskey="U">pyamp.logging</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.logging.prefix</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The prefix module contains the Prefix, FnPrefix, and TimePrefix classes.</span>

<span class="sd">These classes provide the ability to create specific logging prefixes which</span>
<span class="sd">will be printed at the start of a logged message. These prefixes can be things</span>
<span class="sd">such as: the output of a specific function, the current time, or custom</span>
<span class="sd">prefixes.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>

<span class="kn">from</span> <span class="nn">pyamp.logging.colors</span> <span class="kn">import</span> <span class="n">Colors</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;Prefix&quot;</span><span class="p">,</span> <span class="s">&quot;FnPrefix&quot;</span><span class="p">,</span> <span class="s">&quot;TimePrefix&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="Prefix"><a class="viewcode-back" href="../../../_generated/pyamp_logging_prefix.html#pyamp.logging.prefix.Prefix">[docs]</a><span class="k">class</span> <span class="nc">Prefix</span><span class="p">:</span>
    <span class="sd">&#39;&#39;&#39;The Prefix class provides the ability to add chain of prefixes to a</span>
<span class="sd">    message. A Prefix contains text which is printed in square brackets</span>
<span class="sd">    before the given message. The following shows an example of the a</span>
<span class="sd">    Prefix for the string &quot;DEBUG&quot;.</span>

<span class="sd">    Example::</span>

<span class="sd">        prefix = Prefix(text=&quot;DEBUG&quot;)</span>

<span class="sd">        # Prints: [DEBUG]: This is the message</span>
<span class="sd">        prefix.apply(&quot;This is the message&quot;)</span>

<span class="sd">    A Prefix can be colored, and it can contain other prefixes which are</span>
<span class="sd">    appended to the message before adding the current Prefix.</span>

<span class="sd">    An example of multiple prefixes::</span>

<span class="sd">        debugPrefix = Prefix(text=&quot;DEBUG&quot;)</span>
<span class="sd">        examplePrefix = Prefix(text=&quot;example&quot;, prefix=debugPrefix)</span>
<span class="sd">        datePrefix = Prefix(text=&quot;03/11/2012&quot;, prefix=examplePrefix)</span>

<span class="sd">        # Prints: [DEBUG]: [example]: [03/11/2012]: This is the message</span>
<span class="sd">        datePrefix.apply(&quot;This is the message&quot;)</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="c"># Define the default color for prefixes</span>
    <span class="n">DefaultColor</span> <span class="o">=</span> <span class="n">Colors</span><span class="o">.</span><span class="n">Foreground</span><span class="o">.</span><span class="n">White</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">DefaultColor</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * text -- The text to display in the Prefix</span>
<span class="sd">        * color -- The color in which the Prefix will be displayed</span>
<span class="sd">        * prefix -- The next Prefix in the chain</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__text</span> <span class="o">=</span> <span class="n">text</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__color</span> <span class="o">=</span> <span class="n">color</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__prefix</span> <span class="o">=</span> <span class="n">prefix</span>

<div class="viewcode-block" id="Prefix.apply"><a class="viewcode-back" href="../../../_generated/pyamp_logging_prefix.html#pyamp.logging.prefix.Prefix.apply">[docs]</a>    <span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">useColors</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Apply the chain of Prefixes to the given message.</span>

<span class="sd">        * message -- The message to apply the Prefix chain to</span>
<span class="sd">        * useColors -- True to use colors, False otherwise</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># First apply the sub prefix, is there is one</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__prefix</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__prefix</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">useColors</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_addPrefix</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__text</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">useColors</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">_addPrefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">useColors</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Add the given prefix text to the message.</span>

<span class="sd">        * text -- The prefix text to add</span>
<span class="sd">        * message -- The message to which the prefix will be added</span>
<span class="sd">        * useColors -- True to use colors, false otherwise</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="n">text</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">text</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;[</span><span class="si">%s</span><span class="s">]: &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__colorize</span><span class="p">(</span><span class="n">text</span><span class="p">)</span> <span class="k">if</span> <span class="n">useColors</span> <span class="k">else</span> <span class="n">text</span><span class="p">)</span>

        <span class="k">return</span> <span class="s">&quot;</span><span class="si">%s%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">message</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__colorize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Colorize the given message using our current color.</span>

<span class="sd">        * message -- The message to colorize</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="s">&quot;</span><span class="si">%s%s%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__getColor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__color</span><span class="p">),</span>
                           <span class="n">message</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getColor</span><span class="p">(</span><span class="n">Prefix</span><span class="o">.</span><span class="n">DefaultColor</span><span class="p">))</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__getColor</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">color</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get the color string for the given color.</span>

<span class="sd">        * color -- The color</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="s">&quot;</span><span class="si">%s%d%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">Colors</span><span class="o">.</span><span class="n">Tag</span><span class="p">,</span> <span class="n">color</span><span class="p">,</span> <span class="n">Colors</span><span class="o">.</span><span class="n">Symbol</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="FnPrefix"><a class="viewcode-back" href="../../../_generated/pyamp_logging_prefix.html#pyamp.logging.prefix.FnPrefix">[docs]</a><span class="k">class</span> <span class="nc">FnPrefix</span><span class="p">(</span><span class="n">Prefix</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The FnPrefix class provides to ability to create a :class:`Prefix` whose</span>
<span class="sd">    text results from calling a specified function when the :class:`Prefix` is</span>
<span class="sd">    applied to a message.</span>

<span class="sd">    Example::</span>

<span class="sd">        prefix = FnPrefix(function=lambda: &quot;Hey!&quot;)</span>

<span class="sd">        # Prints: [Hey!]: This is the message</span>
<span class="sd">        prefix.apply(&quot;This is the message&quot;)</span>

<span class="sd">    This class can be used to dynamically create the text used for a prefix by</span>
<span class="sd">    calling a function which returns the desired value for the prefix.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">Prefix</span><span class="o">.</span><span class="n">DefaultColor</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * function -- The function which returns the Prefix text</span>
<span class="sd">        * color -- The color in which the Prefix will be displayed</span>
<span class="sd">        * prefix -- The next Prefix in the chain</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">Prefix</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__function</span> <span class="o">=</span> <span class="n">function</span>

    <span class="k">def</span> <span class="nf">_addPrefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Override the _addPrefix base class method.</span>

<span class="sd">        * text -- The prefix text to add</span>
<span class="sd">        * message -- The message to which the prefix will be added</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="n">Prefix</span><span class="o">.</span><span class="n">_addPrefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__function</span><span class="p">(),</span> <span class="n">message</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="TimePrefix"><a class="viewcode-back" href="../../../_generated/pyamp_logging_prefix.html#pyamp.logging.prefix.TimePrefix">[docs]</a><span class="k">class</span> <span class="nc">TimePrefix</span><span class="p">(</span><span class="n">FnPrefix</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The TimePrefix provides the ability to add a :class:`Prefix` which</span>
<span class="sd">    displays the date and or time when the :class:`Prefix` is applied to the</span>
<span class="sd">    message in a specified format.</span>

<span class="sd">    Example::</span>

<span class="sd">        prefix = TimePrefix(format=&quot;%Y-%m-%d&quot;)</span>

<span class="sd">        # Prints: [2012-03-22]: This is the message</span>
<span class="sd">        prefix.apply(&quot;This is the message&quot;)</span>

<span class="sd">    This class is useful for dynamically timestamping logged messages so they</span>
<span class="sd">    can easily be tracked in the system output.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">format</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">Prefix</span><span class="o">.</span><span class="n">DefaultColor</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * format -- The format in which the time will be displayed. See the</span>
<span class="sd">                    man page for &#39;date&#39;</span>
<span class="sd">        * color -- The color in which the Prefix will be displayed</span>
<span class="sd">        * prefix -- The next prefix in the chain</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__format</span> <span class="o">=</span> <span class="n">format</span>
        <span class="n">FnPrefix</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getTime</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getTime</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Return the current time in the specified time format.&#39;&#39;&#39;</span>
        <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__format</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">now</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__format</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">now</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../index.html" >Module code</a> &raquo;</li>
          <li><a href="../logging.html" >pyamp.logging</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>